<?php namespace App\Http\Controllers\Api;

use App\Exceptions\NotFoundException;
use App\Exceptions\SecurityException;
use App\Exceptions\SignInException;
use App\Facilitator;
use App\User;
use Illuminate\Auth\Guard;
use Illuminate\Support\Facades\Input;
use Response;
use Illuminate\Support\Str;

class FacController extends ApiController
{

    public function setFacInfo(Guard $guard){
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User $user */
        $user = $guard->user();
        $userId = $user->id;
        $facInfo = Facilitator::whereUserId($userId)->first();
        if(!$facInfo){
            $facInfo = new Facilitator();
            $facInfo->user()->associate($user);
        }

        $nickname     = Input::get('nickname');
        $realname     = Input::get('realname');
        $province     = Input::get('s_province');
        $city         = Input::get('s_city');
        $area         = Input::get('s_county');
        $telephone    = Input::get('telephone');
        $qq           = Input::get('qq');
        $email        = Input::get('email');
        $cardNum       = Input::get('card_num');
        $img_photo_id = Input::get('photo_id');
        $img_cardf_id = Input::get('cardf_id');
        $img_cardb_id = Input::get('cardb_id');
        $state        = Input::get('state');
        $tState       = Input::get('t_state');
        $invite       = Input::get('invite');

        if(!is_null($nickname))$facInfo->nickname = $nickname;
        if(!is_null($realname))$facInfo->realname = $realname;
        if(!is_null($province))$facInfo->province = $province;
        if(!is_null($city)) $facInfo->city = $city;
        if(!is_null($area)) $facInfo->area = $area;
        if(!is_null($telephone))$facInfo->telephone = $telephone;
        if(!is_null($qq))$facInfo->qq = $qq;
        if(!is_null($email))$facInfo->email = $email;
        if(!is_null($img_photo_id)) $facInfo->img_photo_id = $img_photo_id;
        if(!is_null($cardNum))$facInfo->card_num = $cardNum;
        if(!is_null($img_cardf_id))$facInfo->img_cardf_id = $img_cardf_id;
        if(!is_null($img_cardb_id))$facInfo->img_cardb_id = $img_cardb_id;
        if(!is_null($state))$facInfo->state = $state;
        if(!is_null($tState))$facInfo->t_state = $tState;

        $facInfo->save();

        $fac = $facInfo->whereInviteNum($invite)->first();
        if($fac){
            $oldTags = $facInfo->followParent()->lists('pid');
            if(!$oldTags){
                $facInfo->followParent()->attach($fac->id);
            }else{
                $facInfo->followParent()->detach($oldTags);
                $facInfo->followParent()->attach($fac->id);
            }
        }

        return $this->buildResponse(trans('api.set_fac_info.success'),$facInfo);
    }

    public function getFacInfo(Guard $guard){
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User $user */
        $user = $guard->user();

        $facInfo = $user->facilitator;

        return $this->buildResponse(trans('api.get.facinfo.success'),$facInfo);
    }

    public function getPhotoByUserId($userId)
    {
        $user = User::find($userId);
        $facInfo = Facilitator::whereUserId($userId)->first();

        if(!$facInfo)
            return Response::download('images/userlogo.jpg','userlogo.jpg',[],'inline');

        if(!$facInfo->img_photo_id)
            return Response::download('images/userlogo.jpg','userlogo.jpg',[],'inline');

        if(!$user)
            throw new NotFoundException(NotFoundException::UserNotFound);

        $photo = $facInfo->photo;

        if(!$photo || $photo == null)
            return Response::download('images/userlogo.jpg','userlogo.jpg',[],'inline');

        $photoFile = $photo->files;

        if(!$photoFile || $photoFile == null)
            return Response::download('images/userlogo.jpg','userlogo.jpg',[],'inline');

        return Response::download($photoFile->getLocalPath(),$photoFile->file_name,[],'inline');
    }

    public function getCardFByUserId($userId)
    {
        $user = User::find($userId);
        $facInfo = Facilitator::whereUserId($userId)->first();

        if(!$facInfo)
            return Response::download('images/id1.jpg','userlogo.jpg',[],'inline');

        if(!$facInfo->img_cardf_id)
            return Response::download('images/id1.jpg','userlogo.jpg',[],'inline');

        if(!$user)
            throw new NotFoundException(NotFoundException::UserNotFound);

        $photo = $facInfo->cardf;

        if(!$photo || $photo == null)
            return Response::download('images/id1.jpg','userlogo.jpg',[],'inline');

        $photoFile = $photo->files;

        if(!$photoFile || $photoFile == null)
            return Response::download('images/id1.jpg','userlogo.jpg',[],'inline');

        return Response::download($photoFile->getLocalPath(),$photoFile->file_name,[],'inline');
    }

    public function getCardBByUserId($userId)
    {
        $user = User::find($userId);
        $facInfo = Facilitator::whereUserId($userId)->first();

        if(!$facInfo)
            return Response::download('images/id2.jpg','userlogo.jpg',[],'inline');

        if(!$facInfo->img_cardb_id)
            return Response::download('images/id2.jpg','userlogo.jpg',[],'inline');

        if(!$user)
            throw new NotFoundException(NotFoundException::UserNotFound);

        $photo = $facInfo->cardb;

        if(!$photo || $photo == null)
            return Response::download('images/id2.jpg','userlogo.jpg',[],'inline');

        $photoFile = $photo->files;

        if(!$photoFile || $photoFile == null)
            return Response::download('images/id2.jpg','userlogo.jpg',[],'inline');

        return Response::download($photoFile->getLocalPath(),$photoFile->file_name,[],'inline');
    }
}